package deibay.server.frontend.communication.clients;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Level;

import deibay.DeibayLogger;
import deibay.messages.Message;
import deibay.users.User;
import deibay.util.IClosable;
import deibay.util.Pair;

public class MessageQueue implements IClosable{
	
	private BlockingQueue<Pair<User, Message>> messageQueue = null;
	private MessageConsumer consumer = null;
	private Thread tConsumer;
	
	public MessageQueue()
	{
		messageQueue = new LinkedBlockingQueue<Pair<User, Message>>();
		consumer = new MessageConsumer(messageQueue);				
	}
	
	public void init()
	{
		initializeThreads();	
	}
	
	private void initializeThreads()
	{				
		tConsumer = new Thread(consumer);
		tConsumer.start();			
	}
	
	public synchronized void putMessageToQueue(User user, Message message)
	{				
		messageQueue.offer(new Pair<User, Message>(user, message));
	}

	public void close() {
		consumer.close();		
	}	
}
